package com.dao.impl;

import com.dao.BookDao;
import com.entity.Book;
import com.util.DBUtil;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class BookDaoImpl implements BookDao {
    private static final String DELETE_BOOK_SQL = "delete from book where bookid=?";
    private static final String SELECT_BOOKBYID_SQL = "select bookid,bookname,publish,author,bookcount,booktype from book where bookid=?";
    private static final String UPDATA_BOOKBY_SQL = "update book set %s where bookid=%d ";

    @Override
    public boolean deleteBookById(long id) throws Exception {
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement ps = connection.prepareStatement(DELETE_BOOK_SQL)
        ) {
            ps.setLong(1, id);
            int i = ps.executeUpdate();
            if (i >= 1) {
                return true;
            } else {
                return false;
            }

        }
    }

    @Override
    public Book getBookById(long id) throws Exception {
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement ps = connection.prepareStatement(SELECT_BOOKBYID_SQL)
        ) {
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                Book bookFromResource = getBookFromResource(rs);
                System.out.println(bookFromResource.getBookid()+"---------");
                return bookFromResource;
            } else {
                return null;
            }

        }
    }

    @Override
    public Book updataBook(Book book,long id) throws Exception {
        List<String> segments = new ArrayList<>();
        List<Object> params = new ArrayList<>();

        if (Objects.nonNull(book.getBookname()) && !book.getBookname().isEmpty()) {
            segments.add("bookname=?");
            params.add(book.getBookname());
        }
        if (Objects.nonNull(book.getAuthor()) && !book.getAuthor().isEmpty()) {
            segments.add("author=?");
            params.add(book.getAuthor());
        }
        if (Objects.nonNull(book.getPublish()) && !book.getPublish().isEmpty()) {
            segments.add("publish=?");
            params.add(book.getPublish());
        }
        if (Objects.nonNull(book.getBookcount())) {
            segments.add("bookcount=?");
            params.add(book.getBookcount());
        }
        if (Objects.nonNull(book.getBooktype()) && !book.getBooktype().isEmpty()) {
            segments.add("booktype=?");
            params.add(book.getBooktype());
        }
        String sql = String.format(UPDATA_BOOKBY_SQL, String.join(",", segments), id);
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            for (int i = 0; i < params.size(); i++) {
                Object param = params.get(i);
                if (param instanceof java.util.Date) {
                    java.util.Date date = (java.util.Date) param;
                    ps.setDate(i + 1, new Date(date.getTime()));
                } else if(param instanceof Long){
                    ps.setLong(i + 1, (Long) param);
                }else {
                    ps.setString(i + 1, (String) param);
                }
            }

            ps.executeUpdate();
            return getBookById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private  static  Book getBookFromResource(ResultSet rs)throws Exception {
        Book book=new Book();
        book.setBookid(rs.getLong("bookid"));
        book.setBookname(rs.getString("bookname"));
        book.setPublish(rs.getString("publish"));
        book.setAuthor(rs.getString("author"));
        book.setBookcount(rs.getLong("bookcount"));
        book.setBooktype(rs.getString("booktype"));
        return  book;
    }
}
